<?php

namespace addons\shopro\controller;

use fast\ExpressBird;

class OrderExpress extends Base
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    public function index()
    {
        $params = $this->request->get();

        $this->success('包裹列表', \addons\shopro\model\OrderExpress::getList($params));
    }

    public function detail()
    {
        $params = $this->request->get();
        $result = \addons\shopro\model\OrderExpress::detail($params);
        // 快递轨迹查询
        $expressBird = new ExpressBird();
        $order       = \addons\shopro\model\Order::where(['id' => $params['order_id']])
            ->field(['id', 'consignee', 'phone', 'address'])
            ->find();
        $ret         = $expressBird->queryTrack($result['express_no'], $order->phone);
        $trackList   = [];

        // 重新设置快递状态
        if ($ret['Success']) {
            $trackList = $ret['Traces'];
            if (!empty($trackList)) {
                foreach ($trackList as $k => &$v) {
                    if ($v['Action'] == 202) {
                        $trackList[$k - 2]['Action'] = 3;
                        $trackList[$k - 1]['Action'] = 4;
                    }
                }
            }
        }

        $result['trackList'] = $trackList;

        $this->success('包裹详情', $result);
    }
}
